JavaScript

Datediff Method

Syntax

Date.diff([scope,[date[,format]]])

Arguments

scopestringarray

The scope of difference. This can be "years", "months", "weeks", "days", "hours", "minutes" and/or "seconds". If an array is passed in, the returned object will divide the difference between the scope units. A string with a "-" separating two units will output all units between and including the two specified, for example "weeks-hours" would output "weeks", "days" and "hours".

datestringdatearray

The date to compare the given date with. If no value is passed in the date will be compared to the current date.

formatstring

If the passed in date is a string, this will specify the format the date is in. If no format is specified the default format in A5.d.date.format will be used. See Date.toFormat for a full list of format values.

Returns

diffobject

The difference between the dates.

unitsarray

All the units ("years", "months", "weeks", "days", "hours", "minutes" and/or "seconds") used in the difference.

beforeboolean

Whether or not the date variable the Date.diff method is called on is before the passed in date.

yearsnumber

The number of years difference. This will only be included if the difference scope includes this unit.

monthsnumber

The number of months difference. This will only be included if the difference scope includes this unit.

weeksnumber

The number of weeks difference. This will only be included if the difference scope includes this unit.

daysnumber

The number of days difference. This will only be included if the difference scope includes this unit.

hoursnumber

The number of hours difference. This will only be included if the difference scope includes this unit.

minutesnumber

The number of minutes difference. This will only be included if the difference scope includes this unit.

secondsnumber

The number of seconds difference. This will only be included if the difference scope includes this unit.

Description

Extension to the native date variable to allow for checking the difference between the date variable and a specified date or the current date.

Discussion

The Date.diff method returns the difference between the date variable and another date.

The scope controls what difference values are returned. If a scope (such as "years") is not present then that scope will not be included. This means that if "years" is excluded, but "months" is included, and the difference in the dates is more then a year, the "months" will be greater than 12.

Multiple scopes must be defined in the correct order - i.e. "years", "months", "weeks", "days", "hours", "minutes" and "seconds". If no scope is specified, the scope is set to "days", "hours", and "minutes".

Example

var d = new Date(2018,7,10);
var d2 = new Date(2018,8,5,12,30,20,100);
var dif = d.diff('months-days');
// dif = {months: 0, weeks: 3, days: 5, before: true}
dif = d.diff(['days','minutes'],d2);
// {days: 26, minutes: 750, before: true}
dif = d.diff('hours',d2);
// dif = {hours: 636, before: true}